﻿using System;
using Microsoft.Extensions.DependencyInjection;
using NetMicro.EventBus.BackgroundServices;
using NetMicro.EventBus.Persistences;

namespace NetMicro.EventBus.MySql
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 使用连接字符串初始化注册mysql存储
        /// </summary>
        /// <param name="eventBusBuilder"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IEventBusBuilder AddMySql(
            this IEventBusBuilder eventBusBuilder, Action<EventBusMySqlOptions> options)
        {
            eventBusBuilder.Services.Configure(options);
            eventBusBuilder.Services.AddSingleton<IDelayMessageStorage, DelayMessageStorage>();
            eventBusBuilder.Services.AddSingleton<IMessageStorageInitializer, MySqlMessageStorageInitializer>();
            eventBusBuilder.Services.AddSingleton<IRetryMessageStorage, RetryMessageStorage>();
            eventBusBuilder.Services.AddSingleton<IEventDelayMessageServiceProvider, EventDelayMessageServiceProvider>();
            eventBusBuilder.Services.AddSingleton<IMessageFixServiceProvider, MessageFixServiceProvider>();
            eventBusBuilder.Services.AddSingleton<IDeadLetterMessageStorage, DeadLetterMessageStorage>();
            eventBusBuilder.Services.AddScoped<ITransactionContext, TransactionContext>();
            return eventBusBuilder;
        }
    }
}
